package operations;

import book.BookList;

import java.util.Scanner;

public class DelOperation implements IOperations{

    @Override
    public void work(BookList bookList) {
//        System.out.println("删除图书");
        System.out.println("请输入你要删除的书名");
        Scanner scanner = new Scanner(System.in);
        String delName = scanner.nextLine();
        int length = bookList.getUsedSize();
        int index = -1;
        for (int i = 0; i < length; i++) {
            if (bookList.getBook(i).getName().equals(delName)){
                index = i;
                break;
            }
        }
        if (index == -1){
            System.out.println("找不到这本书");
            return;
        }
        //如果是j < length走到最后一步 j++ 会越界
        for (int i = index; i < length - 1; i++) {
            bookList.setBooks(i,bookList.getBook(i+1));
        }
        bookList.setUsedSize(length - 1);
        System.out.println("删除成功");
    }
}
